package com.visual.avplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;

import com.orhanobut.logger.Logger;

import java.io.IOException;

/**
 * Content：后台播放音乐达到保活目的
 * Actor：韩小呆 ヾ(✿ﾟ▽ﾟ)ノ
 * Time:  2018/10/12 10:47
 * Update:
 * Time:
 */
public class SingASongService extends Service {
    private MediaPlayer mMediaPlayer;
    private Thread thread;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            MyThread myThread = new MyThread();
            thread = new Thread(myThread);
            mMediaPlayer = MediaPlayer.create(MainApplication.getInstance(), R.raw.no_kill);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource("http://abv.cn/music/红豆.mp3");
            mMediaPlayer.prepare();
            mMediaPlayer.setLooping(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Logger.e("onCreate（） 创建播放对象：" + mMediaPlayer.hashCode());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        thread.start();
        Logger.e("播放时 线程名称：" + thread.getName());
        return START_STICKY;
    }

    //开始、暂停播放
    private void startPlaySong() {
        if (mMediaPlayer == null) {
            mMediaPlayer = MediaPlayer.create(MainApplication.getInstance(), R.raw.no_kill);
            Logger.e("音乐启动播放,播放对象为： " + mMediaPlayer.hashCode());
            mMediaPlayer.start();
        } else {
            mMediaPlayer.start();
            Logger.e("音乐启动播放,播放对象为： " + mMediaPlayer.hashCode());
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (mMediaPlayer != null) {
//            mMediaPlayer.pause();
            Logger.e("音乐启动播放,播放对象为： " + mMediaPlayer.hashCode());
            int progress = mMediaPlayer.getCurrentPosition();
            Logger.e("音乐暂停，播放进度：" + progress);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.pause();
        Logger.e("恢复播放 时当前播放器对象：" + mMediaPlayer.hashCode());
        stopPlaySong();
        Logger.e("应用播放服务被杀死，正在重启");
        Logger.e("目标播放工作线程是否存活：" + thread.isAlive());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(new Intent(getApplicationContext(), SingASongService.class));
        } else {
            startService(new Intent(getApplicationContext(), SingASongService.class));
        }
    }

    //停止播放销毁对象
    private void stopPlaySong() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            Logger.e("音乐停止播放,播放对象为：" + mMediaPlayer.hashCode());
            Logger.e("音乐播放器是否在循环：" + mMediaPlayer.isLooping());
            Logger.e("音乐播放器是否还在播放：" + mMediaPlayer.isPlaying());
            mMediaPlayer.release();
            Logger.e("播放对象销毁,播放对象为：" + mMediaPlayer.hashCode());
            mMediaPlayer = null;
        }
    }

    class MyThread implements Runnable {

        @Override
        public void run() {
            startPlaySong();
        }
    }
}
//---------------------
//作者：韩小呆
//来源：CSDN
//原文：https://blog.csdn.net/xiangshiweiyu_hd/article/details/83058516
//版权声明：本文为博主原创文章，转载请附上博文链接！